package org.jeecg.modules.scm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.scm.dto.EditInvItemRsDto;
import org.jeecg.modules.scm.entity.ScmFinaContract;
import org.jeecg.modules.scm.entity.ScmInvItemRs;
import org.jeecg.modules.scm.mapper.ScmInvItemRsMapper;
import org.jeecg.modules.scm.service.IScmInvItemRsService;
import org.jeecg.modules.scm.service.IScmInvestorService;
import org.jeecg.modules.scm.vo.InvItemRsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 资金方项目关联关系控制层类
 * @Author: zzq
 * @Date: 2021-10-21
 * @Version: V1.0
 */
@Api(tags = "资金方项目关联关系")
@RestController
@RequestMapping("/scm/invItemRs")
@Slf4j
public class ScmInvItemRsController extends JeecgController<ScmInvItemRs, IScmInvItemRsService> {
    @Autowired
    private IScmInvItemRsService invItemRsService;

    @Autowired
    private ScmInvItemRsMapper scmInvItemRsMapper;

    @Autowired
    private IScmInvestorService scmInvestorService;

    /**
     * 分页列表查询
     *
     * @param scmInvItemRs
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "invItemRs-分页列表查询")
    @ApiOperation(value="invItemRs-分页列表查询", notes="invItemRs-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(ScmInvItemRs scmInvItemRs,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<ScmInvItemRs> queryWrapper = QueryGenerator.initQueryWrapper(scmInvItemRs, req.getParameterMap());
        Page<InvItemRsVo> page = new Page<InvItemRsVo>(pageNo, pageSize);
        IPage<InvItemRsVo> pageList = scmInvItemRsMapper.queryPage(queryWrapper, page);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "添加", notes = "invItemRs-添加")
    @PostMapping(value = "/add")
    public Result<Integer> add(@RequestBody EditInvItemRsDto dto) {
        int id = invItemRsService.save(dto);
        if (id == 0) {
            return Result.error("添加失败", 0);
        }
        return Result.OK(id);
    }

    /**
     *  编辑
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "invItemRs-编辑")
    @ApiOperation(value = "编辑", notes = "invItemRs-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody EditInvItemRsDto dto) {
        invItemRsService.update(dto);
        return Result.OK("编辑成功!");
    }

    /**
     *  资金方报文编辑
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "invItemRs-报文编辑")
    @PutMapping(value = "/editMsg")
    public Result<?> editMsg(@RequestBody EditInvItemRsDto dto) {
        List<ScmInvItemRs> list = invItemRsService.lambdaQuery().eq(ScmInvItemRs::getInvestorId, dto.getInvestorId())
                .eq(ScmInvItemRs::getItemId, dto.getItemId())
                .list();
        if(list == null||list.size()!=1){
            throw new JeecgBootException("未查找到对应关系");
        }
        dto.setId(list.get(0).getId());
        invItemRsService.update(dto);
        return Result.OK("编辑成功!");
    }




    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "invItemRs-通过id删除")
    @ApiOperation(value = "通过id删除", notes = "invItemRs-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        invItemRsService.removeById(id);
        return Result.OK("删除成功!");
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "invItemRs-通过id查询")
    @ApiOperation(value = "通过id查询", notes = "invItemRs-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        ScmInvItemRs scmInvItemRs = invItemRsService.getById(id);
        if(scmInvItemRs ==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(scmInvItemRs);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param scmInvItemRs
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ScmInvItemRs scmInvItemRs) {
        return super.exportXls(request, scmInvItemRs, ScmInvItemRs.class, "scm_investor_item_relationship");
    }
    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ScmInvItemRs.class);
    }


    @ApiOperation("接收资金端报文并修改资金方项目关联关系")
    @PostMapping("/updateByInvMsg")
    public Result<Boolean> updateByInvMsg(@RequestBody EditInvItemRsDto editInvItemRsDto) {
        Boolean update = invItemRsService.update(editInvItemRsDto);
        return Result.OK(update);
    }


    @ApiOperation(value = "查看资金方融融资情况", notes = "返回资金方关联的融资合同列表,按照“生效日期”最新排序展现")
    @GetMapping("/queryInvContractList")
    public Result<List<ScmFinaContract>> queryInvContractList(@RequestParam(value = "invId") Integer invId) {
        List<ScmFinaContract> contractList = scmInvestorService.queryInvContractList(invId);
        return Result.OK(contractList);
    }

    @ApiOperation(value = "上传附件", notes = "上传附件")
    @PostMapping("/uploadFile")
    public Result<List<String>> uploadFile(MultipartFile file, @RequestParam(name = "关联ID") Integer id) {
        String paths = invItemRsService.uploadAccessory(file, id);
        return Result.OK(Arrays.asList(paths.split(",")));
    }

}
